﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ants;
using SettlersEngine;
using System.Drawing;
using System.IO;

namespace AntsBot
{
    public class SpreadOut : Strategy
    {
        public SpreadOut(AntLoc ant) : base(StrategyTypes.SpreadOut, ant) { }

        public override Goal GetGoal()
        {
            if (Ant.State.TimeRemaining < 250)
            {
                Utility.Log("Running out of time, scattering");
                return new Scatter(Ant).GetGoal();
            }
            // try and figure out how close to the edge of vision I am
            var x = Ant.Col;
            var y = Ant.Row;

            var minX = Ant.State.MyAnts.Min(a => a.Col);
            var maxX = Ant.State.MyAnts.Max(a => a.Col);
            var minY = Ant.State.MyAnts.Min(a => a.Row);
            var maxY = Ant.State.MyAnts.Max(a => a.Row);


            var searchingspots = Ant.State.FoodTiles.Where(f => Ant.State.PersistentState.Goals.Count(g => g.EndPoint.Equals(f)) == 0);

            Func<GameState, bool> terminationFunc = (s => searchingspots.Any() || s.EnemyAnts.Any());

            var modifier = Ant.State.ViewRadius2 / 2;
            var movedistance = 2;

            Utility.Log("spread out: minx:" + minX + " miny:" + minY + " maxx:" + maxX + " maxy:" + maxY + " widthmod:" + modifier + " heightmod:" + modifier);
            // near the left (ish)
            if (x < (minX + modifier))
            {
                Utility.Log("moving left");
                var loc = new Location(Ant.State, y, x - movedistance); //move left

                while (!loc.IsPassable() && loc.GetDistance(Ant) < (modifier))
                {
                    loc = new Location(Ant.State, loc.Row, loc.Col - movedistance);
                }

                Utility.Log("left, path :" + Ant + " " + loc);
                var path = Utility.GetPath(Ant, loc, Ant.State);
                if (path != null)
                {
                    return new Goal(Ant, path, terminationFunc, StrategyTypes.SpreadOut);
                }
            }
            else if (x > (maxX - Ant.State.ViewRadius2))
            {
                var loc = new Location(Ant.State, y, x + movedistance); //move right
                while (!loc.IsPassable() && loc.GetDistance(Ant) < (modifier ))
                {
                    loc = new Location(Ant.State, loc.Row, loc.Col + movedistance);
                }

                Utility.Log("right, path :" + Ant + " " + loc);
                var path = Utility.GetPath(Ant, loc, Ant.State);
                if (path != null)
                {
                    return new Goal(Ant, path, terminationFunc, StrategyTypes.SpreadOut);
                }
            }
            else if (y < (minY + modifier))
            {
                var loc = new Location(Ant.State, y - movedistance, x); //move up
                while (!loc.IsPassable() && loc.GetDistance(Ant) < (modifier ))
                {
                    loc = new Location(Ant.State, loc.Row - movedistance, loc.Col);
                }

                Utility.Log("top, path :" + Ant + " " + loc);
                var path = Utility.GetPath(Ant, loc, Ant.State);
                if (path != null)
                {
                    return new Goal(Ant, path, terminationFunc, StrategyTypes.SpreadOut);
                }
            }
            else if (y > (maxY - modifier))
            {
                var loc = new Location(Ant.State, y + movedistance, x); // move down
                while (!loc.IsPassable() && loc.GetDistance(Ant) < (modifier))
                {
                    loc = new Location(Ant.State, loc.Row + movedistance, loc.Col);
                }

                Utility.Log("bottom, path :" + Ant + " " + loc);
                var path = Utility.GetPath(Ant, loc, Ant.State);
                if (path != null)
                {
                    return new Goal(Ant, path, terminationFunc, StrategyTypes.SpreadOut);
                }
            }

            return new Scatter(Ant).GetGoal();
        }
    }
}